﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using WPFDevelopers.Samples.Helpers;
using WPFDevelopers.Samples.Models;
using WPFDevelopers.Samples.ViewModels;

namespace WPFDevelopers.Samples.ExampleViews.Win10Menu
{
    /// <summary>
    /// Win10MenuExample.xaml 的交互逻辑
    /// </summary>
    public partial class Win10MenuExample : Window
    {
        private int dataType = 0;
        public delegate void NumManipulationHandler(int DataType);
        public event NumManipulationHandler ChangeNum;


        protected virtual void OnNumChanged()
        {
            if (ChangeNum != null)
            {
                ChangeNum(dataType); /* 事件被触发 */
            }
            else
            {
                Console.WriteLine("event not fire");
                //Console.ReadKey(); /* 回车继续 */
            }
        }

        public void SetValue()
        {
            OnNumChanged();
        }




        private double normaltop = 0.0;
        private double normalleft = 0.0;
        private double normalwidth = 0.0;
        private double normalheight = 0.0;

        Win10MenuExampleVM menuExampleVM;


        private List<Uri> _uriList = new List<Uri>()
        {
            new Uri("ExampleViews/Win10Menu/HomePage.xaml",UriKind.Relative),
            new Uri("ExampleViews/Win10Menu/EdgePage.xaml",UriKind.Relative),
        };
        public Win10MenuExample()
        {
            InitializeComponent();
            menuExampleVM = new Win10MenuExampleVM();
            this.DataContext = menuExampleVM;
            App.mainWindows = this;
            this.Loaded += (r, s) =>
            {
                this.MouseDown += (x, y) =>
                {
                    if (y.LeftButton == MouseButtonState.Pressed)
                    {
                        this.DragMove();
                    }
                };
            };

        }

        #region 最大化 最小化 关闭

        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void maxBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.border01.CornerRadius = new CornerRadius(0);
                normaltop = this.Top;
                normalleft = this.Left;
                normalwidth = this.Width;
                normalheight = this.Height;
                this.MaxHeight = SystemParameters.WorkArea.Height;
                this.WindowState = WindowState.Maximized;
                dataType = 0;
               
                SetValue();
            }
            else
            {
                this.WindowState = WindowState.Normal;
                this.border01.CornerRadius = new CornerRadius(5);
                this.Top = 0;
                this.Left = 0;
                this.Width = 0;
                this.Height = 0;
                this.Top = normaltop;
                this.Left = normalleft;
                this.Width = normalwidth;
                this.Height = normalheight;
                this.Margin = new Thickness(0);
                dataType = 1;
                SetValue();
            }
        }

        private void minBtn_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }



        #endregion

        public ICommand HomeCommand => new Helpers.RelayCommand(obj =>
        {
            menuExampleVM.GetListItemById(0);

        });
        public ICommand EdgeCommand => new Helpers.RelayCommand(obj =>
        {
            menuExampleVM.GetListItemById(1);
        });
        public ICommand CloudCommand => new Helpers.RelayCommand(obj =>
        {
            menuExampleVM.GetListItemById(2);
        });
        public ICommand MailCommand => new Helpers.RelayCommand(obj =>
        {

        });
        public ICommand VideoCommand => new Helpers.RelayCommand(obj =>
        {

        });

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var model = (System.Windows.FrameworkElement)this.MenuListBox.SelectedItem;
                if (model != null)
                {
                    if (this.MyFrame.CanGoBack == true)
                    {
                        this.MyFrame.RemoveBackEntry();
                    }

                    var objectModel = (MenuMode)model.Tag;
                    if (this.MyFrame.NavigationService != null && objectModel != null && !string.IsNullOrWhiteSpace(objectModel.Url))
                    {
                        this.MyFrame.NavigationService.Navigated += NavServiceOnNavigated;
                        this.MyFrame.Navigate(new Uri(objectModel.Url, UriKind.Relative));
                    }
                }

            }
            catch (Exception)
            {
            }
        }

        private void NavServiceOnNavigated(object sender, NavigationEventArgs e)
        {
            this.MyFrame.NavigationService.RemoveBackEntry();
            this.MyFrame.NavigationService.Navigated -= NavServiceOnNavigated;
        }
    }
}
